<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\TypeModel;
use Illuminate\Http\Request;

class Typecontroller extends Controller
{
    public function types(Request $request)
    {
        $types=TypeModel::get();

        return view("admin.types",compact("types"));
    }

    public function addtype(Request $request)
    {
        return view("admin.addtype");
   }

    public function insertType(Request $request)
    {
$id=$request->input("id");
$typs=TypeModel::find($id);
        $input = $request->input();
        $image = $request->file("image");
        $img_name = "typeimg_" . date("ymd_his", time()) . rand(1, 1000) . "." . $image->getClientOriginalExtension();

        $image->move(public_path('type_image'), $img_name);
        $input['image'] = "type_image/" . $img_name;
        if ($id==0){
            $ok = TypeModel::create($input);
            if ($ok) {
                return $this->success(null, [
                    "title" => "操作成功",
                    "text" => "数据添加成功",
                ]);
            }
        }else{
            $ok = $typs->update($input);
            if ($ok) {
                return $this->success(null, [
                    "title" => "操作成功",
                    "text" => "数据修改成功",
                ]);
            }
        }

    }

    public function updateType(Request $request)
    {
        $id=$request->input("id");
//        dd($id);
        $types=TypeModel::find($id);
        return view("admin.addtype",compact("types"));
    }

    public function deleteType(Request $request)
    {
        $id=$request->input("id");
        $types=TypeModel::find($id)->delete();
        if ($types) {
            return $this->success(null, [
                "title" => "操作成功",
                "text" => "数据修改成功",
            ]);
        }
    }
}
